package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateOrder(c *gin.Context) {
	var req request.CreateOrder
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	list, err := config.UserClient.CreateOrder(c, &__.CreateOrderReq{
		UserId:        int64(req.UserId),
		ReservationId: int64(req.ReservationId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "创建订失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "创建订成功",
		"data":    list,
	})

}

func UpdateOrderStatus(c *gin.Context) {
	var req request.UpdateOrderStatus
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	list, err := config.UserClient.UpdateOrderStatus(c, &__.UpdateOrderStatusReq{
		Id:          int64(req.Id),
		OrderStatus: int64(req.OrderStatus),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "修改订单状态失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "修改订单状态成功",
		"data":    list,
	})

}

func OrderList(c *gin.Context) {
	var req request.OrderList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	list, err := config.UserClient.OrderList(c, &__.OrderListReq{
		MyOrderList: int64(req.MyOrderList),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "订单列表展示失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "订单列表展示成功",
		"data":    list,
	})
}
